import 'package:inmotion_pro/service/realtime_data_server/vehicle_realtime_data/vehicle_data_base.dart';
import 'package:safemap/safemap.dart';

class VehicleStatisticsTotal extends VehicleDataBase {
  // 累计统计数据
  double totalMileage = 0; // 累计里程
  int totalConsumption = 0; // 累计消耗能量 (单位: kJ)
  int totalRecovery = 0; // 累计能量回收
  int totalRideTime = 0; // 累计持续时间
  int totalPowerOnTime = 0; // 累计通电时间

  // totalPowerOnTime 单位为秒，转换为时分秒格式字符串
  String get totalPowerOnTimeString {
    int hours = totalPowerOnTime ~/ 3600;
    int minutes = (totalPowerOnTime % 3600) ~/ 60;
    int seconds = totalPowerOnTime % 60;
    // 当有小时，则只显示小时
    if (hours > 0) {
      return "$hours h";
    } else if (minutes > 0) {
      return "$minutes min";
    } else {
      return "$seconds s";
    }
  }

  // 总消耗能量，单位转换为kW，功率(kW) = 能量(kJ) ÷ 时间(秒)
  double get energyConsumption {
    // 将 kJ 转换为 kW
    if (totalRideTime < 60) return 0;
    return (totalConsumption / totalRideTime).toDouble();
  }

  // 回收能量，单位转换为kW，功率(kW) = 能量(kJ) ÷ 时间(秒)
  double get energyRecovery {
    // 将 kJ 转换为 kW
    if (totalRideTime < 60) return 0;
    return (totalRecovery / totalRideTime).toDouble();
  }

  VehicleStatisticsTotal.fromMap(Map<String, dynamic>? data)
      : super.fromMap(data) {
    SafeMap safeMap = SafeMap(data);
    totalMileage = (safeMap['total_mileage'].toDouble ?? 0) / 100;
    totalConsumption = safeMap['total_consumption'].toInt ?? 0;
    totalRecovery = safeMap['total_recovery'].toInt ?? 0;
    totalRideTime = safeMap['total_ride_time'].toInt ?? 0;
    totalPowerOnTime = safeMap['total_power_on_time'].toInt ?? 0;
  }

  // 当前数据是否有效
  bool get isValid {
    return totalMileage > 0 &&
        totalConsumption > 0 &&
        totalRecovery > 0 &&
        totalRideTime > 0 &&
        totalPowerOnTime > 0;
  }
}
